/**
 * @file mg_air_conditioner.c
 * @author Charry_Le (530673764@qq.com)
 * @brief 子设备空调源文件
 * @version 0.1
 * @date 2024-02-17
 *
 * @copyright Moorgen Intelligent Technologies Copyright (c) 2024
 *
 */
#include "../mg_device.h"
#include "mg_air_conditioner.h"
#include "mg_timer.h"

STRUCT_AIR_VALU Air;
STRUCT_AIR_VALU Air_After_Occupy;
AIR_CONFIG Air_Config;

// 是否在夏季
static bool IsInSummer(void)
{
	bool IsSkip = false; // 跨年标记
	struct tm myTime = Get_Sys_Time();
	if (Air.SeasonMode == SEASON_MODE_SUMMER)
		return true;
	else if (Air.SeasonMode == SEASON_MODE_WINTER)
		return false;
	if (Air_Config.byte.Seasonal_Switch == false)
		return false;
	// 判断是否跨年
	if (Air_Config.byte.Summer_Start_Date.Mouth > Air_Config.byte.Summer_End_Date.Mouth)
		IsSkip = true;
	else if (Air_Config.byte.Summer_Start_Date.Mouth == Air_Config.byte.Summer_End_Date.Mouth &&
			 Air_Config.byte.Summer_Start_Date.Day > Air_Config.byte.Summer_End_Date.Day)
		IsSkip = true;
	if (IsSkip)
	{
		if (((myTime.tm_mon == Air_Config.byte.Summer_End_Date.Mouth && myTime.tm_mday >= Air_Config.byte.Summer_End_Date.Day) ||
			(myTime.tm_mon > Air_Config.byte.Summer_End_Date.Mouth)) &&
			((myTime.tm_mon == Air_Config.byte.Summer_Start_Date.Mouth && myTime.tm_mday <= Air_Config.byte.Summer_Start_Date.Day) ||
			(myTime.tm_mon < Air_Config.byte.Summer_Start_Date.Mouth)))
			return false;
		else
			return true;
	}
	else
	{
		if (((myTime.tm_mon == Air_Config.byte.Summer_Start_Date.Mouth && myTime.tm_mday >= Air_Config.byte.Summer_Start_Date.Day) ||
			(myTime.tm_mon > Air_Config.byte.Summer_Start_Date.Mouth)) &&
			((myTime.tm_mon == Air_Config.byte.Summer_End_Date.Mouth && myTime.tm_mday <= Air_Config.byte.Summer_End_Date.Day) ||
			(myTime.tm_mon < Air_Config.byte.Summer_End_Date.Mouth)))
			return true;
		else
			return false;
	}
}
// 是否在冬季
static bool IsInWinter(void)
{
	bool IsSkip = false; // 跨年标记
	struct tm myTime = Get_Sys_Time();
	if (Air.SeasonMode == SEASON_MODE_SUMMER)
		return false;
	else if (Air.SeasonMode == SEASON_MODE_WINTER)
		return true;
	if (Air_Config.byte.Seasonal_Switch == OFF)
		return false;
	// 判断是否跨年
	if (Air_Config.byte.Winter_Start_Date.Mouth > Air_Config.byte.Winter_End_Date.Mouth)
		IsSkip = true;
	else if (Air_Config.byte.Winter_Start_Date.Mouth == Air_Config.byte.Winter_End_Date.Mouth &&
			 Air_Config.byte.Winter_Start_Date.Day > Air_Config.byte.Winter_End_Date.Day)
		IsSkip = true;
	if (IsSkip)
	{
		if (((myTime.tm_mon == Air_Config.byte.Winter_End_Date.Mouth && myTime.tm_mday >= Air_Config.byte.Winter_End_Date.Day) ||
			(myTime.tm_mon > Air_Config.byte.Winter_End_Date.Mouth)) &&
			((myTime.tm_mon == Air_Config.byte.Winter_Start_Date.Mouth && myTime.tm_mday <= Air_Config.byte.Winter_Start_Date.Day) ||
			(myTime.tm_mon < Air_Config.byte.Winter_Start_Date.Mouth)))
			return false;
		else
			return true;
	}
	else
	{
		if (((myTime.tm_mon == Air_Config.byte.Winter_Start_Date.Mouth && myTime.tm_mday >= Air_Config.byte.Winter_Start_Date.Day) ||
			(myTime.tm_mon > Air_Config.byte.Winter_Start_Date.Mouth)) &&
			((myTime.tm_mon == Air_Config.byte.Winter_End_Date.Mouth && myTime.tm_mday <= Air_Config.byte.Winter_End_Date.Day) ||
			(myTime.tm_mon < Air_Config.byte.Winter_End_Date.Mouth)))
			return true;
		else
			return false;
	}
}

// 获取温控器温度最小值
float Get_Air_TempRange_Min(void)
{
	if (IsInSummer()) // 执行夏季温度范围
		return (float)Air_Config.byte.Summer_Air_Temp_Range.Min / 2;
	else if (IsInWinter()) // 执行冬季季温度范围
		return (float)Air_Config.byte.Winter_Air_Temp_Range.Min / 2;
	else
		return (float)Air_Config.byte.Air_Temp_Range.Min / 2;
}
// 获取温控器温度最大值
float Get_Air_TempRange_Max(void)
{
	if (IsInSummer()) // 执行夏季温度范围
		return (float)Air_Config.byte.Summer_Air_Temp_Range.Max / 2;
	else if (IsInWinter()) // 执行冬季季温度范围
		return (float)Air_Config.byte.Winter_Air_Temp_Range.Max / 2;
	else
		return (float)Air_Config.byte.Air_Temp_Range.Max / 2;
}
// 模式是否支持
uint8_t Mode_Is_Support(ENUM_AIR_MODE Mode)
{
	if (Mode == 0 || Mode > MODE_HEAT_TYPE)
		return 0;
	return ((Air_Config.byte.Custom_Mode >> (Mode - 1)) & 0x01);
}
// 模式是否使能
uint8_t Mode_Is_Enable(ENUM_AIR_MODE Mode)
{
	switch (Mode)
	{
	case MODE_COOL:
		return Air_Config.byte.Mode_function.Mode_Cool_En;
	case MODE_HEAT:
		return Air_Config.byte.Mode_function.Mode_Heat_En;
	case MODE_PREHEAT:
		return Air_Config.byte.Mode_function.Mode_Preheat_En;
	case MODE_HEAT_PLUS:
		return Air_Config.byte.Mode_function.Mode_HeatPlus_En;
	case MODE_HEAT_TYPE:
		return Air_Config.byte.Mode_function.Mode_HeatType_En;
	default:
		return 1;
	}
}
// 风速是否支持
uint8_t Fan_Is_Support(ENUM_AIR_WIND_SPEED Speed)
{
	return ((Air_Config.byte.Custom_Wind >> Speed) & 0x01);
}
// 垂直风向是否支持
uint8_t Vertical_Dir_Support(ENUM_AIR_WIND_DIRECTION_VER Direction)
{
	if (Direction == WIND_DIRECTION_VER_NONE)
		return 1;
	return ((Air_Config.byte.Custom_Vertical_Dir >> (Direction - 1)) & 0x01);
}
// 水平风向是否支持
uint8_t Level_Dir_Is_Support(ENUM_AIR_WIND_DIRECTION_LEVEL Direction)
{
	if (Direction == WIND_DIRECTION_LEVEL_NONE)
		return 1;
	return ((Air_Config.byte.Custom_Level_Dir >> (Direction - 1)) & 0x01);
}

/**********************************************************************/
/******************************空调控制********************************/
/**********************************************************************/
// 空调开关控制
void Air_Switch_Set(bool Switch)
{
	if (Air.Switch == Switch)
		return;
	Air.Switch = Switch;
}

// 空调温度控制
void Air_Temp_Set(float Temperature)
{
	float Set_Temp;
	if (Temperature > Get_Air_TempRange_Max())
		Set_Temp = Get_Air_TempRange_Max();
	else if (Temperature < Get_Air_TempRange_Min())
		Set_Temp = Get_Air_TempRange_Min();
	else
		Set_Temp = Temperature;
	Air.UserTemp = Set_Temp; // 用户设置温度
}
// 空调温度加
void Air_Temp_Add(void)
{
	float n, temp;
	temp = Air.UserTemp;
	n = temp - (uint16_t)temp;
	if ((n != 0) && (n != 0.5))
		temp = (uint16_t)temp;
	if (temp >= Get_Air_TempRange_Max())
	{
		temp = Get_Air_TempRange_Max();
		return;
	}
	if (Air_Config.byte.Air_Temp_Range.Grads)
	{
		temp = (int)temp;
		temp += 1;
	}
	else
	{
		temp += 0.5;
	}
	Air.UserTemp = temp;
}
// 空调温度减
void Air_Temp_Sub(void)
{
	float n, temp;
	temp = Air.UserTemp;
	n = temp - (uint16_t)temp;
	if ((n != 0) && (n != 0.5))
		temp = (uint16_t)temp;
	if (temp <= Get_Air_TempRange_Min())
	{
		temp = Get_Air_TempRange_Min();
		return;
	}
	if (Air_Config.byte.Air_Temp_Range.Grads)
	{
		temp = (int)temp;
		temp -= 1;
	}
	else
	{
		temp -= 0.5;
	}
	Air.UserTemp = temp;
}
// 空调模式控制
void Air_Mode_Set(ENUM_AIR_MODE Mode)
{
	if (Mode < MODE_COOL || Mode > MODE_HEAT_TYPE)
		return;
	if (!Mode_Is_Support(Mode))
		return;
	if (!Mode_Is_Enable(Mode))
		return;

	Air.Mode = Mode;
	if (Air.Mode == MODE_WIND && Air.WindSpeed == WIND_SPEED_AUTO)
	{
		Air.WindSpeed = WIND_SPEED_LOW; // 风速
	}
}
// 空调模式切换(按键触发)
void Air_Mode_Cut(void)
{
	uint8_t loop_count = 0;
	ENUM_AIR_MODE temp = Air.Mode;
	do
	{
		(temp == MODE_HEAT_TYPE) ? temp = MODE_COOL : temp++;
		loop_count++;
		if (loop_count > 0x10)
			return;
	} while (!(Mode_Is_Support(temp) && Mode_Is_Enable(temp)));
	Air_Mode_Set(temp);
}
// 空调风速控制
void Air_Fan_Set(ENUM_AIR_WIND_SPEED Speed)
{
	if (Speed > WIND_SPEED_FFAST)
		return;
	if (!Fan_Is_Support(Speed))
		return;
	if (Air.Mode == MODE_WIND && Speed == WIND_SPEED_AUTO)
		return;

	Air.WindSpeed = (ENUM_AIR_WIND_SPEED)Speed;
}
// 空调风速-
void Air_Fan_Sub(void)
{
	if (Air_Config.byte.Custom_Wind == 0) // 所有风速都不支持
		return;
	do
	{
		switch (Air.WindSpeed) // 由于协议上风速顺序不符合实际，改用switch
		{
		case WIND_SPEED_AUTO:
			Air.WindSpeed = WIND_SPEED_FFAST;
			break;
		case WIND_SPEED_LOW:
			if (Air.Mode == MODE_WIND)
				Air.WindSpeed = WIND_SPEED_FFAST;
			else
				Air.WindSpeed = WIND_SPEED_AUTO;
			break;
		case WIND_SPEED_MID:
			Air.WindSpeed = WIND_SPEED_LOW;
			break;
		case WIND_SPEED_MID_HIGH:
			Air.WindSpeed = WIND_SPEED_MID;
			break;
		case WIND_SPEED_HIGH:
			Air.WindSpeed = WIND_SPEED_MID_HIGH;
			break;
		case WIND_SPEED_FFAST:
			Air.WindSpeed = WIND_SPEED_HIGH;
			break;
		default:
			Air.WindSpeed = WIND_SPEED_LOW;
			break;
		}
	} while (!Fan_Is_Support(Air.WindSpeed)); // 对风速有效性进行查询
}
// 空调风速+
void Air_Fan_Add(void)
{
	if (Air_Config.byte.Custom_Wind == 0) // 所有风速都不支持
		return;
	do
	{
		switch (Air.WindSpeed) // 由于协议上风速顺序不符合实际，改用switch
		{
		case WIND_SPEED_AUTO:
			Air.WindSpeed = WIND_SPEED_LOW;
			break;
		case WIND_SPEED_LOW:
			Air.WindSpeed = WIND_SPEED_MID;
			break;
		case WIND_SPEED_MID:
			Air.WindSpeed = WIND_SPEED_MID_HIGH;
			break;
		case WIND_SPEED_MID_HIGH:
			Air.WindSpeed = WIND_SPEED_HIGH;
			break;
		case WIND_SPEED_HIGH:
			Air.WindSpeed = WIND_SPEED_FFAST;
			break;
		case WIND_SPEED_FFAST:
			if (Air.Mode == MODE_WIND)
				Air.WindSpeed = WIND_SPEED_LOW;
			else
				Air.WindSpeed = WIND_SPEED_AUTO;
			break;
		default:
			Air.WindSpeed = WIND_SPEED_LOW;
			break;
		}
	} while (!Fan_Is_Support(Air.WindSpeed)); // 对风速进行有效性查询
}

// 空调垂直风向控制
void Air_Vertical_FanDirection_Set(ENUM_AIR_WIND_DIRECTION_VER Direction)
{
	if (Direction > WIND_DIRECTION_VER_5)
		return;
	if (!Vertical_Dir_Support(Direction))
		return;
	Air.VerWind = Direction;
}

// 空调水平风向控制
void Air_Level_FanDirection_Set(ENUM_AIR_WIND_DIRECTION_LEVEL Direction)
{
	if (Direction > WIND_DIRECTION_LEVEL_5)
		return;
	if (!Level_Dir_Is_Support(Direction))
		return;
	Air.LevelWind = Direction;
}

// 设置空调模式禁使能
void Set_Air_Mode_Enable(ENUM_AIR_MODE Mode, bool Enable)
{
	if (Enable != ON && Enable != OFF)
		return;
	switch (Mode)
	{
	case MODE_COOL:
		Air_Config.byte.Mode_function.Mode_Cool_En = Enable;
		break;
	case MODE_HEAT:
		Air_Config.byte.Mode_function.Mode_Heat_En = Enable;
		break;
	case MODE_PREHEAT:
		Air_Config.byte.Mode_function.Mode_Preheat_En = Enable;
		break;
	case MODE_HEAT_PLUS:
		Air_Config.byte.Mode_function.Mode_HeatPlus_En = Enable;
		break;
	case MODE_HEAT_TYPE:
		Air_Config.byte.Mode_function.Mode_HeatType_En = Enable;
		break;
	default:
		break;
	}
	Save_Air_Portion_Config((uint8_t *)&Air_Config.byte.Mode_function, 1);
}
// 设置季节模式
void Set_Air_Season_Mode(ENUM_SEASON_MODE mode)
{
	if (mode > 2)
		return;
	Air.SeasonMode = mode;
}
